package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.UserLearningGoal;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserLearningGoalMapper {
    
    @Results(id = "goalMap", value = {
        @Result(property = "goalId", column = "goal_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "goalType", column = "goal_type"),
        @Result(property = "targetValue", column = "target_value"),
        @Result(property = "currentValue", column = "current_value"),
        @Result(property = "startDate", column = "start_date"),
        @Result(property = "endDate", column = "end_date"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time"),
        @Result(property = "userName", column = "username")
    })
    @Select("SELECT g.*, u.username FROM user_learning_goals g " +
            "INNER JOIN users u ON g.user_id = u.user_id " +
            "WHERE g.user_id = #{userId}")
    List<UserLearningGoal> findByUserId(@Param("userId") Long userId);
    
    @ResultMap("goalMap")
    @Select("SELECT g.*, u.username FROM user_learning_goals g " +
            "INNER JOIN users u ON g.user_id = u.user_id " +
            "WHERE g.goal_id = #{goalId}")
    UserLearningGoal findById(@Param("goalId") Long goalId);
    
    @Insert("INSERT INTO user_learning_goals (user_id, goal_type, target_value, current_value, " +
            "start_date, end_date, status, create_time, update_time) " +
            "VALUES (#{userId}, #{goalType}, #{targetValue}, #{currentValue}, " +
            "#{startDate}, #{endDate}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "goalId")
    void insert(UserLearningGoal goal);
    
    @Update("UPDATE user_learning_goals SET current_value = #{currentValue}, " +
            "status = #{status}, update_time = NOW() " +
            "WHERE goal_id = #{goalId}")
    void updateProgress(@Param("goalId") Long goalId, 
                       @Param("currentValue") Integer currentValue,
                       @Param("status") String status);
    
    @Select("SELECT g.*, u.username FROM user_learning_goals g " +
            "INNER JOIN users u ON g.user_id = u.user_id " +
            "WHERE g.user_id = #{userId} AND g.status = 'active'")
    List<UserLearningGoal> findActiveGoals(@Param("userId") Long userId);
    
    @Select("SELECT g.*, u.username FROM user_learning_goals g " +
            "INNER JOIN users u ON g.user_id = u.user_id " +
            "WHERE g.user_id = #{userId} AND g.status = 'completed'")
    List<UserLearningGoal> findCompletedGoals(@Param("userId") Long userId);
} 